﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSLib.CodeFormating.Lexems;

namespace PSLib.CodeFormating.Adapters
{
	public class LuaAdapter : LanguageAdapter
	{
		internal LuaAdapter()
		{

		}

		public override LexemType ProcessLexem(string lexem)
		{
			LexemType result = base.ProcessLexem(lexem);
			if (constants.Contains(lexem)) result = LexemType.Constant;
			else if (operators.Contains(lexem)) result = LexemType.Operator;
			return result;
		}

		public override LexemList PostProcessLexemList(LexemList lexemList)
		{
			LexemList result = new LexemList();
			for (int i = 0; i < lexemList.Count; i++)
			{
				if (lexemList.CheckSequence(i, "-", "-", "[","["))
				{
					int lastIndex = lexemList.IndexOfSequence(i, "]", "]");
					if (lastIndex == -1) lastIndex = lexemList.Count;
					else lastIndex += 2;
					lexemList.Replace(i, lastIndex - i, LexemType.Comment);
					i = lastIndex;
				}

				if (lexemList.CheckSequence(i, "-", "-"))
				{
					int lastIndex = lexemList.IndexOfAny(i,LexemType.BreakLine);
					if (lastIndex == -1) lastIndex = lexemList.Count;
					lexemList.Replace(i, lastIndex - i, LexemType.Comment);
					i = lastIndex;
				}
			}

			return base.PostProcessLexemList(lexemList);
		}
		//and,not,or
		private List<string> constants = new List<string>(new string[] { "true", "false", "nil" });
		private List<string> operators = new List<string>(new string[] { "break", "do", "if", "else", "elseif", "for", "function", "in", "local", "repeat", "return", "then", "until", "while", "end" });

	}
}
